package com.mingqijia.gassafety.webserver.service.open.impl;

import com.alibaba.fastjson.JSONObject;
import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.authority.api.resp.config.DictionaryItemRespDTO;
import com.mingqijia.gassafety.authority.dal.helper.ConfigureHolder;
import com.mingqijia.gassafety.webserver.request.openapi.order.OpenOrderCompleteRequest;
import com.mingqijia.gassafety.webserver.request.workorder.asst.OrderCompleteRequest;
import com.mingqijia.gassafety.webserver.service.open.OpenOrderService;
import com.mingqijia.gassafety.webserver.service.workorder.SyncOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: duanhongxiang
 * @Desc:
 * @create: 2024-11-15 16:45
 **/
@Service
@Slf4j
public class OpenOrderServiceImpl implements OpenOrderService {
    @Autowired
    ConfigureHolder holder;

    @Autowired
    SyncOrderService syncOrderService;

    @Override
    public void orderComplete(OpenOrderCompleteRequest request) {
        log.info("openOrderComplete-param:{}", JSONObject.toJSON(request));
        List<DictionaryItemRespDTO> itemRespDTOList = holder.getDictionaryByKey("three_quotient_company", "0");
        String spId = "";
        List<String> finishStatusList = new ArrayList<>();
        List<String> cancelStatusList = new ArrayList<>();
        if (null != itemRespDTOList && !itemRespDTOList.isEmpty()) {
            Map<String, DictionaryItemRespDTO> orgMap = itemRespDTOList.stream().collect(
                    Collectors.toMap(DictionaryItemRespDTO::getName, Function.identity())
            );
            DictionaryItemRespDTO targetDictDto = orgMap.get(request.getOrgId());
            if (null != targetDictDto) {
                spId = targetDictDto.getValue();
                String description = targetDictDto.getDescription();
                JSONObject jsonObject = JSONObject.parseObject(description);
                String finishStatus = jsonObject.getString("finish");
                if (!StringUtils.isEmpty(finishStatus)) {
                    finishStatusList = Arrays.asList(finishStatus.split(","));
                }
                String cancelStatus = jsonObject.getString("cancel");
                if (!StringUtils.isEmpty(cancelStatus)) {
                    cancelStatusList = Arrays.asList(cancelStatus.split(","));
                }
            }
        }
        if (StringUtils.isEmpty(spId)) {
            throw new BizException("根据企业ID未找到第三方映射关系，请确认企业id是不是在第三方系统企业ID映射配置中");
        }
        if (finishStatusList.isEmpty()) {
            throw new BizException("未找到第三方企业的完工状态，请到字典里配置第三方企业的完工状态");
        }
        if (finishStatusList.contains(request.getOrderStatus()) || cancelStatusList.contains(request.getOrderStatus())) {
            OrderCompleteRequest orderCompleteRequest = new OrderCompleteRequest();
            orderCompleteRequest.setOrderNo(request.getOrderNo());
            orderCompleteRequest.setCompletedTime(request.getCompletedTime());
            orderCompleteRequest.setCompleteResult("2");
            orderCompleteRequest.setVisitPerson(request.getOperatorBy());
            orderCompleteRequest.setWorkSystem(4);
            syncOrderService.orderComplete(orderCompleteRequest);
        }else{
            throw new BizException("工单非完结状态，请检查工单状态与字典配置的完结状态是否一致");
        }
    }

}
